package com.neuronbit.businessflow.plugin;

import com.neuronbit.businessflow.plugin.task.CopyDependencyTask;
import com.neuronbit.businessflow.plugin.task.GenerateSourceCodeTask;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.TaskContainer;
import org.jetbrains.annotations.NotNull;

/**
 * The type Plugin.
 */
public class Plugin implements org.gradle.api.Plugin<Project> {
    @Override
    public void apply(@NotNull Project project) {
        try {
            TaskContainer taskContainer = project.getTasks();
            GenerateSourceCodeTask generateFlowSourceCode = taskContainer.create("generateFlowSourceCode", GenerateSourceCodeTask.class);
            generateFlowSourceCode.setGroup("Business Flow");

            Task copyDependencies = taskContainer.findByName("copyDependencies");
            if (copyDependencies == null) {
                copyDependencies = taskContainer.create("copyDependencies", CopyDependencyTask.class);
                copyDependencies.setGroup("Business Flow");
            }

            generateFlowSourceCode.dependsOn(copyDependencies);

            Task compileJavaTask = taskContainer.getByName("compileJava");
            compileJavaTask.dependsOn(generateFlowSourceCode);
        } catch (Exception e) {
            project.getLogger().error("apply business flow tasks to project failed", e);
        }
    }
}
